﻿#include "ui_setLocal.h"
#include "ui_ui_setLocal.h"
#include <QMessageBox>
#include <QDebug>

uiSetLocal::uiSetLocal(LocalDev * ld , QWidget *parent) :
    QWidget(parent),
    ui(new Ui::uiSetLocal)
{
    ui->setupUi(this);

    this->m_xml = new ToolXml;

    this->m_ld = ld;
    Hmi h = m_ld->getHmi();
    this->m_hmi = &h;


    // 设备参数
    ui->busId->setText(m_ld->busID() == 0 ? "总线1" : "总线2");
    ui->maddr->setText(m_ld->getMAddr());
    ui->sMaddr->setText(m_ld->getMAddr());
    ui->baudrate->setCurrentIndex(m_ld->getBaudrate() - 1);
    ui->offset->setText(QString::number(m_ld->offset()));
    ui->logLevel->setCurrentIndex(m_ld->getLogLevel());

    // 位置参数
    ui->row->setText(QString::number(m_ld->row()));
    ui->col->setText(QString::number(m_ld->col()));
    ui->sector->setText(QString::number(m_ld->sector()));
    ui->hcdi->setText(QString::number(m_ld->hcdi()));
    ui->latitude->setText(QString::number(m_ld->getLatitude()));
    ui->longitude->setText(QString::number(m_ld->getLongitude()));
    ui->altitude->setText(QString::number(m_ld->getAltitude()));

    // HMI参数
    ui->busy->setText(QString::number(m_hmi->busy()));
    ui->mode->setText(QString::number(m_hmi->mode()));
    ui->state->setText(QString::number(m_hmi->state()));
    ui->instruct->setText(QString::number(m_hmi->instruct()));
    ui->allowAutiSwi->setText(QString::number(m_hmi->allowAutiSwi()));
    ui->b4->setText(QString::number(m_hmi->b4()));
    ui->reset->setText(QString::number(m_hmi->reset()));
    ui->b5->setText(QString::number(m_hmi->b5()));
    ui->emrst->setText(QString::number(m_hmi->emrst()));
    ui->b6->setText(QString::number(m_hmi->b6()));
    ui->emergencyStop->setText(QString::number(m_hmi->emergencyStop()));
    ui->b7->setText(QString::number(m_hmi->b7()));
    ui->enginnerAuth->setText(QString::number(m_hmi->enginnerAuth()));
    ui->cmd->setText(QString::number(m_hmi->cmd()));
    ui->x->setText(QString::number(m_hmi->x()));
    ui->y->setText(QString::number(m_hmi->y()));
    ui->z->setText(QString::number(m_hmi->z()));
}

uiSetLocal::~uiSetLocal()
{
    delete ui;

    delete this->m_xml;
}

void uiSetLocal::on_pushButton_clicked()
{
    LocalDev * ld = new LocalDev();
    Hmi hmi;
    bool ok;
    bool ret = true;

    // 设备参数
    ld->setBusID(m_ld->busID());
    ld->setMAddr(m_ld->getMAddr());
    ld->setSMAddr(ui->sMaddr->text());
    ld->setBaudrate(ui->baudrate->currentIndex() + 1);
    ld->setOffset(ui->offset->text().toInt(&ok));
    ret = ok ? ret : ok;
    ld->setLogLevel(ui->logLevel->currentIndex());

    // 位置参数
    ld->setRow(ui->row->text().toInt(&ok));
    ret = ok ? ret : ok;
    ld->setCol(ui->col->text().toInt(&ok));
    ret = ok ? ret : ok;
    ld->setSector(ui->sector->text().toInt(&ok));
    ret = ok ? ret : ok;
    ld->setHcdi(ui->hcdi->text().toInt(&ok));
    ret = ok ? ret : ok;
    ld->setLatitude(ui->latitude->text().toDouble(&ok));
    ret = ok ? ret : ok;
    ld->setLongitude(ui->longitude->text().toDouble(&ok));
    ret = ok ? ret : ok;
    ld->setAltitude(ui->altitude->text().toDouble(&ok));
    ret = ok ? ret : ok;

    // HMI参数
    hmi.setBusy(ui->busy->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setMode(ui->mode->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setState(ui->state->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setInstruct(ui->instruct->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setAllowAutiSwi(ui->allowAutiSwi->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setB4(ui->b4->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setReset(ui->reset->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setB5(ui->b5->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setEmrst(ui->emrst->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setB6(ui->b6->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setEmergencyStop(ui->emergencyStop->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setB7(ui->b7->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setEnginnerAuth(ui->enginnerAuth->text().toInt(&ok));
    ret = ok ? ret : ok;
    hmi.setCmd(ui->cmd->text().toInt(&ok));
    ret = ok ? ret : ok;

    if (!ret)
    {
        QMessageBox::warning(this, "参数错误", "设置的参数有误！");
    }
    else
    {
        ld->setHmi(hmi);
        ld->setSn(m_ld->getSn());

        QString xml = m_xml->makeSetLocalXml(ld);

        emit setLocal(xml);

        this->close();
        this->deleteLater();
    }
}
